package com.aacar.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MathUtil {
	public static int getRandomNum() {
		SecureRandom random = new SecureRandom();
		int x = random.nextInt(8999);
		x = x + 1000;
		return x;
	}

	/**
	 * 处理金额
	 * @param amount
	 * @return
	 */
	public static double disposeAmount(double amount) {
		BigDecimal b = new BigDecimal(amount);
		return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	public static double accuracy(double num, double total, int scale){  
		if(total==0){
			return 0;
		}
        DecimalFormat df = (DecimalFormat)NumberFormat.getInstance();  
        //可以设置精确几位小数  
        df.setMaximumFractionDigits(scale);  
        //模式 例如四舍五入  
        df.setRoundingMode(RoundingMode.HALF_UP);  
        double accuracy_num = num / total;  
        return accuracy_num;  
	}  
    
    public static void main(String[] args) {
		System.out.println(accuracy(35,50,0));
	}
}
